package com.sync.syncmasterdata.configuration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.util.Map;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef="entityManagerFactoryFive",
        transactionManagerRef="transactionManagerFive",
        basePackages= {"com.sync.syncmasterdata.vendor.repository.amu8rep","com.sync.syncmasterdata.outsystem.repository","com.sync.syncmasterdata.product.repository.amu8"})//设置dao（repo）所在位置
public class FiveConfig {
    @Autowired
    @Qualifier("dataSourceFive")
    private DataSource secondDataSource;

    @Autowired
    @Qualifier("vendorProperties")
    private Map<String, Object> vendorProperties;


    @Bean(name = "entityManagerFactoryFive")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryFive(EntityManagerFactoryBuilder builder) {
        LocalContainerEntityManagerFactoryBean entityManagerFactory = builder
                .dataSource(secondDataSource)
                .properties(vendorProperties)
                .packages("com.sync.syncmasterdata.vendor.entity.amu8","com.sync.syncmasterdata.outsystem.entity","com.sync.syncmasterdata.product.entity.amu8")
                .persistenceUnit("fivePersistenceUnit")
                .build();
        return entityManagerFactory;
    }

    @Bean(name = "entityManagerFive")
    public EntityManager entityManagerFive(EntityManagerFactoryBuilder builder) {
        return entityManagerFactoryFive(builder).getObject().createEntityManager();
    }


    @Bean(name = "transactionManagerFive")
    PlatformTransactionManager transactionManagerFive(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactoryFive(builder).getObject());
    }

}
